﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.CM.Controllers
{
    public class ViolationController : Controller
    {
        //
        // GET: /CM/Violation/

        public ActionResult ViolationList(string PassCenterCode, string PassStationCode, string PassPlateNumber, string PassCarNumber)
        {
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            this.ViewData["selfWorkCode"] = selfWorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            this.ViewData["selfCenterID"] = selfCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["selfStationID"] = selfStationID;
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng(1262);//判断登录人有无“新增”违章信息权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增违章信息权限
            }

            this.ViewData["start"] = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd");
            this.ViewData["end"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            this.ViewData["name"] = UserOperateContext.Current.Session_UsrInfo.Name;
            this.ViewData["time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (PassCenterCode != "--请选择--" && PassCenterCode != "" && PassCenterCode != null)
            { this.ViewData["txtCenterCode"] = PassCenterCode; };
            if (PassStationCode != "--请选择--" && PassStationCode != "" && PassStationCode != null)
            { this.ViewData["txtStationCode"] = PassStationCode; };
            if (PassPlateNumber != "--请选择--" && PassPlateNumber != "" && PassPlateNumber != null)
            { this.ViewData["txtPlateNumber"] = PassPlateNumber; };
            if (PassCarNumber != "--请选择--" && PassCarNumber != "" && PassCarNumber != null)
            { this.ViewData["txtCarNumber"] = PassCarNumber; };
            return View();
        }

        /// <summary>
        /// 查询列表信息    尤浩 2015-11-27
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DataLoad()
        {
            try
            {
                string start = Request.Form["start"].ToString();
                string end = Request.Form["end"].ToString();
                string center = Request.Form["center"].ToString();
                string station = Request.Form["station"].ToString();
                string actualCode = Request.Form["actualCode"].ToString();
                string ambulanceNumber = Request.Form["ambulanceNumber"].ToString();
                string place = Request.Form["place"].ToString();
                string driverWorkID = Request.Form["driverWorkID"].ToString();
                string driverName = Request.Form["driverName"].ToString();

                //获取页容量
                int rows = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int page = int.Parse(Request.Form["page"]);
                E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
                string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
                string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;

                var list = new C_ViolationBLL().GetViolationList(page, rows, start, end, center, station, actualCode, ambulanceNumber,
                                                                 place, driverWorkID, driverName, em, selfWorkCode, selfCenterID, selfStationID);
                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                //return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }

            catch (Exception e)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("InfoID", "0");
                dict.Add("InfoMessage", e.Message);
                return this.Json(dict);
            }
        }
        ///// <summary>
        ///// 修改    尤浩 2015-11-27
        ///// </summary>
        ///// <returns></returns>
        [HttpPost]
        public ActionResult ViolationEdit()
        {

            string sDetailInfo = Request.Form["sViolationInfo"].ToString();
            C_Violation model = JsonHelper.GetJsonInfoBy<C_Violation>(sDetailInfo);
            C_OilCardLink info = new C_OilCardRefuelBLL().GetActualCodeLinkList(model.ActualCode);
            model.AmbulanceCode = info.AmbulanceCode;
            model.CenterID = Convert.ToInt32(info.CenterID);
            model.StationID = info.StationID;
            model.FillTime = DateTime.Now;
            int res = new C_ViolationBLL().Modify(model, "CenterID", "StationID", "AmbulanceNumber", "ActualCode", "AmbulanceCode", "ViolationPlace", "ViolationContent",
                                                  "DriverWorkID", "DriverName", "ViolationTime", "DisposeResult", "DisposeTime", "Remark", "FillTime", "FillPerson");
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "修改成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "修改失败", null, null);
        }
        /// <summary>
        /// 新增    尤浩 2015-11-27
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ViolationAdd()
        {
            string sDetailInfo = Request.Form["sViolationInfo"].ToString();
            C_Violation info = JsonHelper.GetJsonInfoBy<C_Violation>(sDetailInfo);
            info.FillTime = DateTime.Now;
            if (info.AmbulanceNumber == null)
            { info.AmbulanceNumber = ""; }
            ////根据车牌号取车辆编码
            string ambulanceCode = new C_ViolationBLL().GetAmbulanceCode(info.ActualCode);
            info.AmbulanceCode = ambulanceCode;
            List<C_Violation> list = new C_ViolationBLL().GetMaxID();
            if (list.Count == 0)
            { info.ID = 1; }
            else
            { info.ID = list[0].ID + 1; }
            int res = new C_ViolationBLL().Add(info);
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
        }

        // 获取分中心    
        [HttpPost]
        public ActionResult GetCenter()
        {
            try
            {
                C_ViolationBLL bll = new C_ViolationBLL();
                var result = new object();
                result = bll.GetCenter();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 根据分中心取分站    
        [HttpPost]
        public ActionResult GetStationByCenter(int centerID)
        {
            try
            {
                C_ViolationBLL bll = new C_ViolationBLL();
                var result = new object();
                result = bll.GetStationByCenter(centerID);
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
    }
}
